package com.hwtx.form.controller;

import com.google.common.collect.Maps;
import com.hwtx.form.annotation.Log;
import com.hwtx.form.annotation.OperationType;
import com.hwtx.form.domain.service.FormService;
import io.geekidea.boot.common.constant.SystemConstant;
import io.geekidea.boot.framework.response.ApiCode;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

import static com.hwtx.form.domain.FormConstants.LOCALE_PARAM;

@Slf4j
@RestController
@Tag(name = "页面管理")
@RequestMapping("/page")
public class PageController {

    @Resource
    private FormService formService;

    @GetMapping("/load/{pageName}")
    @Operation(summary = "获取页面定义")
    @Log(type = OperationType.GET, key = "pageName")
    public String load(@PathVariable String pageName, @RequestHeader(value = "locale", defaultValue = SystemConstant.DEFAULT_LOCALE) String locale) throws Exception {
        if (StringUtils.isEmpty(pageName)) {
            return Api2Result.build(ApiCode.FAIL, "页面ID不能为空", "");
        }
        Map<String, String[]> parameters = Maps.newHashMap();
        parameters.put(LOCALE_PARAM, new String[]{locale});
        return Api2Result.build(ApiCode.SUCCESS, "加载成功", formService.getPageFormDef(pageName, parameters));
    }
}
